﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DDD4Fun.Common
{
    public class CXB
    {
        public static void go()
        {
            //协变测试
            ISalary<Progammer> p = new BaseSalaryCounter<Progammer>();
            PrintSalary(p);

            //逆变测试
            Progammer pp = new Progammer() { Name = "Mike" };
            Manager mm = new Manager() { Name = "MM" };

            Test(pp, mm);
            Test<Employee>(pp, mm);//两种是等价的用法
        }

        static void PrintSalary(ISalary<Employee> e)
        {
            e.Pay();
        }

        static void Test<T>(IMyComparable<T> t1, T t2)
        {
            t1.Compare(t2);//调用了基类的Compare。。
            
        }
    }

    /*
     in 逆变 将父类作为子类进行调用
     out 协变 即将子类作为父类进行调用 T作为返回值，如果将T作为输入参数，这不满足协变定义
     */
    interface ISalary<out T> //协变的实现 IEnumerator<out T>、IQueryable<out T>、IEnumerable<out T>
    {
        //void Pay(T t);
        void Pay();
    }

    interface IMyComparable<in T>
    {
        int Compare(T other);
    }

    class BaseSalaryCounter<T> : ISalary<T>
    {
        #region ISalary<T> Members

        public void Pay()
        {
            Console.WriteLine("what the fuck");
        }

        #endregion
    }

    class Employee : IMyComparable<Employee>
    {
        public string Name { get; set; }

        #region IMyComparable<Employee> Members

        public int Compare(Employee other)
        {
            return Name.CompareTo(other.Name);
        }

        #endregion
    }

    class Progammer : Employee, IMyComparable<Progammer>
    {
        #region IMyComparable<Progammer> Members

        public int Compare(Progammer other)
        {
            return Name.CompareTo(other.Name);
        }

        #endregion
    }

    class Manager : Employee
    {
    }
}
